import logging
import pymysql
from django.http import JsonResponse, HttpResponse
from comment.comviews import com_views, success_response, err_response, format_response
from process.sqlmap import ProcessSqlMap
from utils.dbutils import PymysqlUtil, DB_Config, MysqlTools
from utils.formatutils import get_params
from utils.pageutils import paging_simple
from .sqlmap import *
from comment.comservice import add_filter
from django.db import connection

mysql_tools = MysqlTools(db_config=DB_Config.get("default"))


def last_notice(params):
    dict = {
        "user_id": "user_id"
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    # if not params["user_id"]:
    #     return err_response(210)
    result = mysql_tools.select_one(UserSqlMap.LAST_NOTICE)
    if not result:
        return success_response([])
    result["notice_time"] =result.get("notice_time")
    return success_response(result)


def unfinished_task_list(params):
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    # if not params["user_id"]:
    #     return err_response(210)
    user_name = mysql_tools.select_one(UserSqlMap.FIND_NAME.format(**params))
    params["user_name"] = user_name.get("user_name")
    sql = add_filter(UserSqlMap.UNFINISHED_TASK_LIST, params, ["query_str"], **UserSqlMap.FILTER_PARAMS)
    sql += " order by t1.publish_time desc"
    sql = sql.format(**params)
    result = mysql_tools.select_all(sql)
    if not result:
        return success_response([])
    if params.get("isall") != 1:
        result = paging_simple(result, params.get("page"), params.get("size"))
    for res in result:
        res["star_time"] = str(res["star_time"])
        res["end_time"] = str(res["end_time"])
        res["progress_speed"] = res["progress_speed"] * 100
        if res["priority_level"] == 0:
            res["priority_level_name"] = "低"
        elif res["priority_level"] == 1:
            res["priority_level_name"] = "中"
        else:
            res["priority_level_name"] = "高"
    return success_response(result)


def unfinished_task_num(params):
    dict = {
        "user_id": "user_id"
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    user_name = mysql_tools.select_one(UserSqlMap.FIND_NAME.format(**params))
    params["user_name"] = user_name.get("user_name")
    result = mysql_tools.select_one(UserSqlMap.UNFINISHED_TASK_NUM.format(**params))
    return success_response(result)


def my_unreviewed_num(params):
    '''
    待我审核的流程的数量
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id"
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    my = mysql_tools.select_one(UserSqlMap.check_user_name.format(**params))
    params["dept_id"] = my.get("dept_id")
    params["position_name"] = my.get("position_name")
    sql_result = ProcessSqlMap.my_unreviewed + " order by t1.apply_time desc"
    all_process = mysql_tools.select_all(sql_result.format(**params))
    if isinstance(all_process,JsonResponse):
        return  all_process
    result = []
    for process in all_process:
        if process.get("shenuserzhi") == "部门经理审核":
            if process.get("dept_id") == my.get("dept_id"):
                result.append(process)
        else:
            result.append(process)
    if isinstance(result, JsonResponse):
        return result
    return success_response({"unreviewed_num": len(result)})

    # dict = {
    #     "user_id": "user_id"
    # }
    # params = get_params(params, dict)
    # if isinstance(params, JsonResponse):
    #     return params
    # # for key, val in params.items():
    # #     if not val:
    # #         return err_response(210)
    # my_unreviewed = UserSqlMap.my_unreviewed.format(**params)
    # result = mysql_tools.select_all(my_unreviewed,msg="")
    # if isinstance(result,JsonResponse):
    #     return result
    # if not result:
    #     return success_response({"unreviewed_num": 0})
    # else:
    #     res_data = {"unreviewed_num": len(result)}
    #     logging.info(res_data)
    #     return success_response(res_data)

@format_response
def index(request):
    return com_views(request, {
        "last_notice": last_notice,
        "unfinished_task_list": unfinished_task_list,
        "unfinished_task_num": unfinished_task_num,
        "my_unreviewed_num": my_unreviewed_num
    })
